package org.example.config;

import lombok.extern.slf4j.Slf4j;
import org.example.interceptor.JwtTokenUserInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;


    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                    "/api/questionnaires/**",    // 排除问卷相关API
                    "/api/user-responses/**",    // 排除问卷回答相关API
                    "/doc.html",                 // Knife4j文档页面
                    "/webjars/**",               // Knife4j静态资源
                    "/swagger-resources/**",     // Swagger资源
                    "/v3/api-docs/**",           // OpenAPI 3.0文档
                    "/v2/api-docs/**",           // Swagger 2.0文档
                    "/favicon.ico"               // 网站图标
                );
    }

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/**")
                .addResourceLocations("classpath:/static/image/", "file:./image/")
                .setCachePeriod(3600);
    }
}
